import 'package:OASys/components/scaffoldItem.dart';
import 'package:OASys/utils/strUtils.dart';
import 'package:flutter/material.dart';

class PwdPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScaffoldItem('修改密码', MyPwdPage());
  }
}

class MyPwdPage extends StatefulWidget {
  @override
  _MyPwdPageState createState() => _MyPwdPageState();
}

class _MyPwdPageState extends State<MyPwdPage> {
  GlobalKey<FormState> formKey = GlobalKey<FormState>();

  String oldPwd;
  String newPwd;
  String confimPwd;
  bool isShowOldPwd;
  bool isShowNewPwd;
  bool isShowConfimPwd;

  @override
  void initState() {
    super.initState();
    oldPwd = '';
    newPwd = '';
    confimPwd = '';
    isShowOldPwd = true;
    isShowNewPwd = true;
    isShowConfimPwd = true;
  }

  void save() {
    var form = formKey.currentState;
    if (form.validate()) {
      form.save();
      if (!StrUtils.equals(newPwd, confimPwd)) {
        print('密码不一致');
      }
    }
  }

  void reset() {
    var form = formKey.currentState;
    form.reset();
    setState(() {
      oldPwd = '';
      newPwd = '';
      confimPwd = '';
      isShowOldPwd = true;
      isShowNewPwd = true;
      isShowConfimPwd = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 300,
      width: 350,
      margin: EdgeInsets.all(45),
      child: Card(
        child: Container(
          margin: EdgeInsets.all(20),
          child: Row(
            children: [
              Column(
                children: [
                  PrefixContanier('新密码：'),
                  PrefixContanier('旧密码：'),
                  PrefixContanier('确认密码：'),
                ],
              ),
              Container(
                width: 200,
                height: 300,
                child: Form(
                  key: formKey,
                  child: Column(
                    children: [
                      TextFormField(
                        cursorHeight: 30,
                        obscureText: isShowOldPwd,
                        style: TextStyle(
                          fontSize: 16,
                          letterSpacing: 1,
                        ),
                        decoration: InputDecoration(
                          hintText: '输入旧密码',
                          suffixIcon: IconButton(
                            icon: Icon(Icons.remove_red_eye_sharp),
                            onPressed: () {
                              setState(() {
                                isShowOldPwd = !isShowOldPwd;
                              });
                            },
                          ),
                        ),
                        // ignore: missing_return
                        validator: (content) {
                          if (content.length > 8) {
                            return "密码过长";
                          } else if (content.length <= 0) {
                            return "密码过短";
                          }
                        },
                        onSaved: (value) {
                          oldPwd = value;
                        },
                      ),
                      TextFormField(
                        cursorHeight: 30,
                        obscureText: isShowNewPwd,
                        style: TextStyle(
                          fontSize: 16,
                          letterSpacing: 1,
                        ),
                        decoration: InputDecoration(
                          hintText: '输入新密码',
                          suffixIcon: IconButton(
                            icon: Icon(Icons.remove_red_eye_sharp),
                            onPressed: () {
                              setState(() {
                                isShowNewPwd = !isShowNewPwd;
                              });
                            },
                          ),
                        ),
                        // ignore: missing_return
                        validator: (content) {
                          if (content.length > 8) {
                            return "密码过长";
                          } else if (content.length <= 0) {
                            return "密码过短";
                          }
                        },
                        onSaved: (value) {
                          newPwd = value;
                        },
                      ),
                      TextFormField(
                        cursorHeight: 30,
                        obscureText: isShowConfimPwd,
                        style: TextStyle(
                          fontSize: 16,
                          letterSpacing: 1,
                        ),
                        decoration: InputDecoration(
                          hintText: '输入确认密码',
                          suffixIcon: IconButton(
                            icon: Icon(Icons.remove_red_eye_sharp),
                            onPressed: () {
                              setState(() {
                                isShowConfimPwd = !isShowConfimPwd;
                              });
                            },
                          ),
                        ),
                        // ignore: missing_return
                        validator: (content) {
                          if (content.length > 8) {
                            return "密码过长";
                          } else if (content.length <= 0) {
                            return "密码过短";
                          }
                        },
                        onSaved: (value) {
                          confimPwd = value;
                        },
                      ),
                      SizedBox(
                        height: 30,
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          RaisedButton(
                            color: Color.fromRGBO(33, 150, 243, 1),
                            onPressed: save,
                            child: Text(
                              "保 存",
                              style: TextStyle(color: Colors.white),
                            ),
                          ),
                          RaisedButton(
                            color: Color.fromRGBO(103, 194, 58, 1),
                            onPressed: reset,
                            child: Text(
                              "重 置",
                              style: TextStyle(color: Colors.white),
                            ),
                          )
                        ],
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

class PrefixContanier extends StatelessWidget {
  final String label;

  PrefixContanier(this.label, {Key key});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50,
      child: Text(
        label,
        style: TextStyle(
          height: 2.4,
          color: Colors.black,
          fontSize: 16,
          letterSpacing: 1,
        ),
      ),
    );
  }
}
